package com.qjs.smterp.web.filter;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.Logger;

/**
 * Simple filter that prints a report in the log file whenever the associated
 * servlets or JSP pages are accessed.
 */
public class LogFilter implements Filter {
	private static Logger logger = Logger.getLogger("Logging");
	protected FilterConfig config;
	@SuppressWarnings("unused")
	private ServletContext context;
	private String filterName;

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws ServletException, IOException {
		HttpServletRequest req = (HttpServletRequest) request;
		logger.info(req.getRemoteHost() + " tried to access "
				+ req.getRequestURL() + " on " + new Date() + ". "
				+ "(Reported by " + filterName + ".)");
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		this.config = config; // In case it is needed by subclass.
		context = config.getServletContext();
		filterName = config.getFilterName();
	}

	public void destroy() {
	}
}
